package approve.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import model.pojo.Employee;
import nguyen.dao.ListDocuments;
import resource.pojo.Approvedoc;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class PurchaseOrderForm extends Activity{
	EditText title, description, content_current,content_next;
	Button submitBtn;
	String lang = "EN", database = "admin";

	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
		}
		return database;
	}

	Bundle language, bd;
	Approvedoc appDoc;
	ArrayList<String> arr = new ArrayList<String>();
	ListDocuments call = new ListDocuments();
	ArrayList<Employee> list = new ArrayList<Employee>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.approve_purchaseorderform);
		ges = (GetEmps)new GetEmps().execute("");
		initFormLoad();
	}

	public String getLang() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			lang = extras.getString("lang");
		}
		return lang;
	}

	public void initFormLoad() {
		bd = getIntent().getExtras();
		language = getIntent().getExtras().getBundle(getLang());
		DateFormat date = new SimpleDateFormat("dd");
		DateFormat fullDate = new SimpleDateFormat("yyyy.MM.dd");
		
		Calendar current = Calendar.getInstance();
		current.set(Calendar.WEEK_OF_YEAR, current.get(Calendar.WEEK_OF_YEAR));
		current.set(Calendar.DAY_OF_WEEK, current.getFirstDayOfWeek());
		int firstDateOfWeek = Integer.parseInt(date.format(current.getTime()));
		
		String thisWeek = fullDate.format(current.getTime());
		current.set(Calendar.DATE, firstDateOfWeek + 6);
		thisWeek += " - " + fullDate.format(current.getTime());		
		current.set(Calendar.WEEK_OF_YEAR, (current.get(Calendar.WEEK_OF_YEAR) + 1 ));
		current.set(Calendar.DAY_OF_WEEK, current.getFirstDayOfWeek());
		firstDateOfWeek = Integer.parseInt(date.format(current.getTime()));
		
		String nextWeek = fullDate.format(current.getTime());
		current.set(Calendar.DATE, firstDateOfWeek + 6);
		nextWeek += " - " + fullDate.format(current.getTime());		
		title = (EditText) findViewById(R.id.weekly_title);
		title.setHint(language.getString("title"));
		
		description = (EditText) findViewById(R.id.weekly_description);
		description.setHint(language.getString("Description"));
		
		content_current = (EditText) findViewById(R.id.weekly_current);
		content_current.setHint(language.getString("resultOfThisWeek")+" ( "+thisWeek+" )");
		
		content_current = (EditText) findViewById(R.id.weekly_nexttime);
		content_current.setHint(language.getString("resultOfThisWeek")+" ( "+nextWeek+" )");
		
		submitBtn = (Button) findViewById(R.id.weekly_btnsubmit);
		submitBtn.setText(language.getString("document_send"));
		submitBtn.setOnClickListener(submitSend);
	}

	private OnClickListener submitSend = new OnClickListener() {

//		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			title = (EditText) findViewById(R.id.weekly_title);
			description = (EditText) findViewById(R.id.weekly_description);
			content_current = (EditText) findViewById(R.id.weekly_current);
			content_next = (EditText) findViewById(R.id.weekly_nexttime);
			String titleApp = title.getText().toString();
			String descriptionApp = description.getText().toString();
			String contentCurrent = "<P>" + content_current.getText().toString() + "</P>";
			String contentNext = "<P>" + content_next.getText().toString() + "</P>";
			Date date = new Date();
			String idDoc = "" + date.getTime();
			String IDGroup = "" + date.getTime();
			Employee emp = (Employee) getIntent().getExtras().get("em");
			appDoc = new Approvedoc();
			appDoc.setiDApproveDoc(idDoc);
			appDoc.setTitle(titleApp);
			appDoc.setiDGroupApprove(IDGroup);
			appDoc.setDescription(descriptionApp);
			appDoc.setContent(contentCurrent);
			appDoc.setContent2(contentNext);
			appDoc.setiDEmployee(emp.getIdemployee());
			appDoc.setEmployeePerforment(emp.getFullName());
			appDoc.setiDTypeDoc(16);
			showDialog(0);
		}
	};
	GetEmps ges;
	private class GetEmps extends AsyncTask<String, Void, String>{

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			getAllEmpsName();
			return null;
		}
		
	}
	public synchronized void getAllEmpsName() {
		list = call.getAllEmp(bd.getString("database").toString());
		for (int i = 0; i < list.size(); i++) {
			arr.add(list.get(i).getFullName());
		}
	}

	String[] _options;
	protected boolean[] _selections;
	Integer count = 0;
	ArrayList<String> emps = new ArrayList<String>();

	@Override
	protected Dialog onCreateDialog(int id) {
		return new AlertDialog.Builder(this)
				.setTitle(R.string.approver)
				.setMultiChoiceItems(loadOptions(), loadSelections(),
						new DialogSelectionClickHandler())
				.setPositiveButton("OK", new DialogButtonClickHandler())
				.create();
	}

	public String[] loadOptions() {
		_options = new String[arr.size()];
		for (int i = 0; i < arr.size(); i++) {
			_options[i] = arr.get(i);
		}
		return _options;
	}

	public boolean[] loadSelections() {
		_selections = new boolean[_options.length];
		return _selections;
	}

	public class DialogSelectionClickHandler implements
			DialogInterface.OnMultiChoiceClickListener {
		public void onClick(DialogInterface dialog, int clicked,
				boolean selected) {
			if (selected == true) {
				emps.add(_options[clicked].toString());
				count++;
			} else {
				for (int i = 0; i < emps.size(); i++) {
					if (_options.toString().equals(emps.get(i))) {
						emps.remove(i);
						count--;
					}
				}
			}
		}
	}

	public class DialogButtonClickHandler implements
			DialogInterface.OnClickListener {
		public void onClick(DialogInterface dialog, int clicked) {
			switch (clicked) {
			case DialogInterface.BUTTON_POSITIVE:
				ad = (AddDoc) new AddDoc().execute("");
				Intent myIntent = new Intent(PurchaseOrderForm.this,
						MainList.class);
				myIntent.putExtra("database", getDatabase());
				myIntent.putExtra("lang", getLang());
				myIntent.putExtras(bd);
				startActivity(myIntent);
				finish();
				break;
			}
		}
	}
	AddDoc ad;
	private class AddDoc extends AsyncTask<String, Void, String>{

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			addDocument();
			return null;
		}
		
	}	
	public synchronized void addDocument() {
		call.addWeeklyReport(appDoc,getDatabase());
		String[] selects = new String[count];
		int j = 0;
		for (int i = 0; i < emps.size(); i++) {
			for (Employee emp : list) {
				if (emp.getFullName().equals(emps.get(i).toString())) {
					emps.set(i, emp.getIdemployee());
					selects[j] = emps.get(i).toString();
					j++;
				}
			}
		}
		call.addApprover(appDoc.getiDGroupApprove(), selects,getDatabase());
	}

	public ArrayList<Employee> getList() {
		return list;
	}

	public void setList(ArrayList<Employee> list) {
		this.list = list;
	}
}
